<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="订单编号" prop="orderNum">
          <el-input
            v-model="queryParams.orderNum"
            placeholder="请输入订单编号"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="制单人" prop="orderPerson">
          <el-input
            v-model="queryParams.orderPerson"
            placeholder="请输入制单人"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="订单状态" prop="orderStatus">
          <el-select v-model="queryParams.orderStatus" placeholder="请选择订单状态" clearable>
            <el-option
              v-for="dict in order_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['medicine:order:add']"
          >新增新订单</el-button>
        </el-col>

        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="getAllCartDrug"
            v-hasPermi="['medicine:order:add']"
          >查询购物车详情</el-button>

        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['medicine:order:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>


        <!-- 表单提交 表单提交表单提交表单提交表单提交表单提交表单提交表单提交表单提交-->
      <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="订单编号" align="center" prop="orderNum" >
          <template #default="scope">
               <router-link :to="'/medicine/detail/index/' + scope.row.orderId" class="link-type">
                  <span>{{ scope.row.orderNum }}</span>
               </router-link>
            </template>
        </el-table-column>
    
        <el-table-column label="供应商ID" align="center" prop="supplierId" />

        <el-table-column label="订单状态" align="center" prop="orderStatus">
          <template #default="scope">
            <dict-tag :options="order_status" :value="scope.row.orderStatus"/>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="status" >
          <template #default="scope">
               <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
            </template>
        </el-table-column>

        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" v-if="scope.row.orderStatus == 2" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['medicine:order:edit']">修改| |</el-button>
             <span  v-else > **修改**</span>

             <el-button link type="primary" v-if="scope.row.orderStatus == 2" icon="Edit" @click="handleSubmit(scope.row)" v-hasPermi="['medicine:order:edit']">提交订单| |</el-button>
             <span  v-else > **提交订单**</span>

             <el-button link type="primary" v-if="scope.row.orderStatus == 1" icon="Edit" @click="handleCaiGou(scope.row)" v-hasPermi="['medicine:order:edit']">确认采购| |</el-button>
             <span  v-else > **确认采购**</span>

             <el-button link type="primary" v-if="scope.row.orderStatus == 11" icon="Edit" @click="handleIsOrNotException(scope.row)" v-hasPermi="['medicine:order:edit']">采购异常| |</el-button>
             <span  v-else > **采购异常**</span>

             <el-button link type="primary" v-if="scope.row.orderStatus == 11" icon="Edit" @click="handleOver(scope.row)" v-hasPermi="['medicine:order:edit']">待检验| |</el-button>
             <span  v-else > **待检验**</span>
          </template>
        </el-table-column>
      </el-table>
      
      <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
  
      <!-- 添加或修改订单信息对话框 -->
      <el-dialog :title="title" v-model="open" width="500px" append-to-body>
        <el-form ref="orderRef" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="订单编号" prop="orderNum" disabled>
            <el-input v-model="form.orderNum" placeholder="请输入订单编号" disabled/>
          </el-form-item>

    
        <el-form-item label="经销商" prop="supplierId" disabled>
            <el-select v-model="form.supplierId" placeholder="请选择经销商">
              <el-option
              v-for="supplier in supplierList"
            :key="supplier.supplierId"
            :label="supplier.supplierName"
            :value="supplier.supplierId"
              ></el-option>
            </el-select>
          </el-form-item>



          
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </template>
  
  <script setup name="Order">
  import { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/his/order";
  
  const router = useRouter()
  const { proxy } = getCurrentInstance();
  const { order_status,sys_normal_disable } = proxy.useDict('order_status',"sys_normal_disable");
  
  const orderList = ref([]);
  const open = ref(false);
  const loading = ref(true);
  const showSearch = ref(true);
  const ids = ref([]);
  const single = ref(true);
  const multiple = ref(true);
  const total = ref(0);
  const title = ref("");
  
  const data = reactive({
    form: {},
    queryParams: {
      pageNum: 1,
      pageSize: 10,
      orderNum: null,
      orderPerson: null,
      supplierId: null,
      orderStatus: null,
      status: null,
    },
    rules: {
    }
  });
  
  const { queryParams, form, rules } = toRefs(data);
  
  /** 查询订单信息列表 */
  function getList() {
    loading.value = true;
    listOrder(queryParams.value).then(response => {
      orderList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    });
  }
  
  // 取消按钮
  function cancel() {
    open.value = false;
    reset();
  }
  
  // 表单重置
  function reset() {
    form.value = {
      orderId: null,
      orderNum: null,
      orderPerson: null,
      supplierId: null,
      orderMoney: null,
      orderStatus: null,
      status: null,
      createBy: null,
      createTime: null,
      updateBy: null,
      updateTime: null,
      remark: null
    };
    proxy.resetForm("orderRef");
  }
  
  /** 搜索按钮操作 */
  function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
  }
  
  /** 重置按钮操作 */
  function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
  }
  
  // 多选框选中数据
  function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.orderId);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
  }
  
  /** 新增按钮操作 */
  function handleAdd() {
    // reset();
    // open.value = true;
    // title.value = "添加订单信息";
    router.push('/medicine/orderAdd/addOrder')

  }
  
  /** 修改按钮操作 */
  function handleUpdate(row) {
    reset();
    const _orderId = row.orderId || ids.value
    getOrder(_orderId).then(response => {
      form.value = response.data;
      open.value = true;
      title.value = "修改订单信息";
    });
  }
  
  /** 提交按钮 */
  function submitForm() {
    proxy.$refs["orderRef"].validate(valid => {
      if (valid) {
        if (form.value.orderId != null) {
          updateOrder(form.value).then(response => {
            proxy.$modal.msgSuccess("修改成功");
            open.value = false;
            getList();
          });
        } else {
          addOrder(form.value).then(response => {
            proxy.$modal.msgSuccess("新增成功");
            open.value = false;
            getList();
          });
        }
      }
    });
  }
  
  /** 删除按钮操作 */
  function handleDelete(row) {
    const _orderIds = row.orderId || ids.value;
    proxy.$modal.confirm('是否确认删除订单信息编号为"' + _orderIds + '"的数据项？').then(function() {
      return delOrder(_orderIds);
    }).then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    }).catch(() => {});
  }
  
  /** 导出按钮操作 */
  function handleExport() {
    proxy.download('medicine/order/export', {
      ...queryParams.value
    }, `order_${new Date().getTime()}.xlsx`)
  }

  function getAllCartDrug(){
  router.push('/medicine/orderAdd/carDrugTail')
}
  
  getList();


  function handleSubmit(row){
    const data = {
      orderId:row.orderId,
      orderStatus:0
    }
    proxy.$modal.confirm('是否确认提交订单信息编号为"' + data.orderId + '"的数据项？').then(function() {
      return  updateOrder(data);
    }).then(() => {
      getList();
      proxy.$modal.msgSuccess("提交订单成功");
    }).catch(() => {});
  }

  function handleCaiGou(row){
    const data = {
      orderId:row.orderId,
      orderStatus:11
    }
    proxy.$modal.confirm('是否确认采购订单信息编号为"' + data.orderId + '"的数据项？').then(function() {
      return  updateOrder(data);
    }).then(() => {
      getList();
      proxy.$modal.msgSuccess("确认采购订单成功");
    }).catch(() => {});
  }

  function handleIsOrNotException(row){
    const data = {
      orderId:row.orderId,
      orderStatus:3
    }
    proxy.$modal.confirm('是否确认采购订单信息编号为"' + data.orderId + '"的数据项出现异常？').then(function() {
      return  updateOrder(data);
    }).then(() => {
      getList();
      proxy.$modal.msgSuccess("采购订单异常！");
    }).catch(() => {});
  }

  function handleOver(row){
    const data = {
      orderId:row.orderId,
      orderStatus:5
    }
    proxy.$modal.confirm('是否确认采购订单信息编号为"' + data.orderId + '"的数据项没有问题？').then(function() {
      return  updateOrder(data);
    }).then(() => {
      getList();
      proxy.$modal.msgSuccess("采购订单待检验中！");
    }).catch(() => {});
  }
  </script>
  